import { Injectable } from '@angular/core';
import { CoreModule } from '../core.module';
import { ApiService, urlService } from '../api';
import { ReqWithdrawal, CommonResponse, ResRequestWithdrawal, UserTransaction } from '../../model';
import { CommonService } from './common.service';
import { environment } from 'src/environments/environment';
import { retry } from 'rxjs/operators';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: CoreModule
})
export class TransactionService implements CommonService {

  requestUserTransactionForWithdrawal(params: ReqWithdrawal): Observable<CommonResponse<ResRequestWithdrawal>> {
    return this.api.post<CommonResponse<ResRequestWithdrawal>>(this.getUrl('转账请求'), params).pipe(
      retry(3)
    );
  }

  userTransaction(params: {
    transactionId: string;
  }) {
    return this.api.post<CommonResponse<UserTransaction>>(this.getUrl('转账详情'), params).pipe(
      retry(3)
    );
  }

  error(error: any) {
    this.api.goToWhere(error);
  }

  getUrl(apiName: string): string {
     return urlService(environment.type, apiName);
  }

  constructor(
    private api: ApiService
  ) { }
}
